package com.hgh.wallet_demo.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hgh.wallet_demo.Dto.Result;
import com.hgh.wallet_demo.Mapper.UserWalletDao;
import com.hgh.wallet_demo.Mapper.UserWalletDepositDao;
import com.hgh.wallet_demo.Service.UserWalletDepositService;
import com.hgh.wallet_demo.entity.UserWalletDepositEntity;
import com.hgh.wallet_demo.entity.UserWalletEntity;
import com.hgh.wallet_demo.vo.Cost_User;
import com.hgh.wallet_demo.vo.Wallet;
import com.hgh.wallet_demo.vo.deposit_User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service("UserWalletDepositService")
public class UserWalletDepositServiceImpl extends ServiceImpl<UserWalletDepositDao, UserWalletDepositEntity> implements UserWalletDepositService {
    @Autowired
    private UserWalletDepositDao userWalletDepositDao;
    @Autowired
    private UserWalletDao userWalletDao;

    @Override
    @Transactional   //开启事务
    public Result cost(Cost_User costVo) {
        //查找当前用户的余额
        UserWalletEntity userWallet = userWalletDao.selectOne(new QueryWrapper<UserWalletEntity>().eq("user_id", costVo.getUserId()));
        BigDecimal walletAmount = userWallet.getWalletAmount();

        BigDecimal balance = walletAmount.subtract(costVo.getAmount());
        //当钱包余额小于花费的金额时，返回
        if(balance.compareTo(BigDecimal.ZERO) < 0){
            return Result.fail("余额不足");
        }else {
            //添加消费记录
            UserWalletDepositEntity depositEntity = new UserWalletDepositEntity();
            depositEntity.setOldAmount(walletAmount);
            depositEntity.setOrderId(costVo.getOrderId());
            depositEntity.setAmount(costVo.getAmount());
            depositEntity.setType(0);
            depositEntity.setUpdateTime(new Date());
            depositEntity.setUserId(costVo.getUserId());
            userWalletDepositDao.insert(depositEntity);

            //修改钱包数据
            userWallet.setWalletAmount(balance);
            userWallet.setUpdateTime(new Date());
            userWalletDao.updateById(userWallet);
            return Result.ok();
        }
    }


    /**
     * 用户退款20元接口
     * @param refundVo
     */
    @Override
    public void refund(deposit_User refundVo) {
        UserWalletDepositEntity userWalletRecord = new UserWalletDepositEntity();
        BigDecimal walletAmount = userWalletDao.selectOne(new QueryWrapper<UserWalletEntity>().eq("user_id", refundVo.getUserId())).getWalletAmount();
        userWalletRecord.setOldAmount(walletAmount);
        userWalletRecord.setUserId(refundVo.getUserId());
        userWalletRecord.setAmount(refundVo.getAmount());
        userWalletRecord.setOrderId(refundVo.getOrderId());
        userWalletRecord.setType(1);
        userWalletRecord.setUpdateTime(new Date());
        userWalletDepositDao.insert(userWalletRecord);

        //修改钱包金额
        UserWalletEntity userWallet = userWalletDao.selectOne(new QueryWrapper<UserWalletEntity>().eq("user_id", refundVo.getUserId()));
        userWallet.setWalletAmount(userWallet.getWalletAmount().add(refundVo.getAmount()));
        userWallet.setUpdateTime(new Date());
        userWalletDao.updateById(userWallet);
    }




    @Override
    public List<Wallet> listbyUserId(Long userId) {
        List<Wallet> list = userWalletDepositDao.listbyUserId(userId);
        return list;
    }
}

